| | | 1 | | // Copyright (c) 2020-2023 dotBunny Inc. |
| | | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using GDX.Collections.Generic; |
| | | 8 | | |
| | | 9 | | namespace GDX |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// <see cref="GDX.Collections.Generic.SimpleList{T}" /> Based Extension Methods |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// Methods found in this extensions class are less performant then the included methods in |
| | | 16 | | /// <see cref="GDX.Collections.Generic.SimpleList{T}" />. They are seperated out to clearly delineate this |
| | | 17 | | /// performance regression. |
| | | 18 | | /// </remarks> |
| | | 19 | | public static class SimpleListExtensions |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Add an item to the <see cref="SimpleList{T}" /> without checking the internal size, |
| | | 23 | | /// making sure that the item is not already contained in the <see cref="SimpleList{T}" />. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" /> to add to.</param> |
| | | 26 | | /// <param name="targetItem">The target class object to add.</param> |
| | | 27 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 28 | | /// <returns>true/false if the operation was able to add the item successfully.</returns> |
| | | 29 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 30 | | public static bool AddUncheckedUniqueItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) |
| | | 31 | | where T : class |
| | 9 | 32 | | { |
| | 9 | 33 | | if (targetSimpleList.ContainsItem(targetItem)) |
| | 1 | 34 | | { |
| | 1 | 35 | | return false; |
| | | 36 | | } |
| | | 37 | | |
| | 8 | 38 | | targetSimpleList.AddUnchecked(targetItem); |
| | 7 | 39 | | return true; |
| | 8 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Add an object reference to the <see cref="SimpleList{T}" /> without checking the internal size, |
| | | 44 | | /// making sure that the reference is not already contained in the <see cref="SimpleList{T}" />. |
| | | 45 | | /// Does not prevent addition of different objects for which Equals returns true. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" /> to add to.</param> |
| | | 48 | | /// <param name="targetReference">The target class object to add.</param> |
| | | 49 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 50 | | /// <returns>true/false if the operation was able to add the reference successfully.</returns> |
| | | 51 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 52 | | public static bool AddUncheckedUniqueReference<T>(ref this SimpleList<T> targetSimpleList, T targetReference) |
| | | 53 | | where T : class |
| | 9 | 54 | | { |
| | 9 | 55 | | if (targetSimpleList.ContainsReference(targetReference)) |
| | 1 | 56 | | { |
| | 1 | 57 | | return false; |
| | | 58 | | } |
| | | 59 | | |
| | 8 | 60 | | targetSimpleList.AddUnchecked(targetReference); |
| | 7 | 61 | | return true; |
| | 8 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Add an item to the <see cref="SimpleList{T}" /> with checking the internal size (expanding as necessary) |
| | | 66 | | /// making sure that the item is not already contained in the <see cref="SimpleList{T}" />. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" /> to add to.</param> |
| | | 69 | | /// <param name="targetItem">The target class object to add.</param> |
| | | 70 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 71 | | /// <returns>true/false if the operation was able to add the item successfully.</returns> |
| | | 72 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 73 | | public static bool AddWithExpandCheckUniqueItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) |
| | | 74 | | where T : class |
| | 8 | 75 | | { |
| | 8 | 76 | | if (targetSimpleList.ContainsItem(targetItem)) |
| | 1 | 77 | | { |
| | 1 | 78 | | return false; |
| | | 79 | | } |
| | | 80 | | |
| | 7 | 81 | | targetSimpleList.AddWithExpandCheck(targetItem); |
| | 7 | 82 | | return true; |
| | 8 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Add an item to the <see cref="SimpleList{T}" /> with checking the internal size (expanding as necessary) |
| | | 87 | | /// making sure that the item is not already contained in the <see cref="SimpleList{T}" />. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" /> to add to.</param> |
| | | 90 | | /// <param name="targetItem">The target class object to add.</param> |
| | | 91 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 92 | | /// <returns>true/false if the operation was able to add the item successfully.</returns> |
| | | 93 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 94 | | public static bool AddWithExpandCheckUniqueValue<T>(ref this SimpleList<T> targetSimpleList, T targetItem) |
| | | 95 | | where T : IEquatable<T> |
| | 8 | 96 | | { |
| | 8 | 97 | | int count = targetSimpleList.Count; |
| | 34 | 98 | | for (int i = 0; i < count; i++) |
| | 10 | 99 | | { |
| | 10 | 100 | | if (targetSimpleList.Array[i].Equals(targetItem)) |
| | 1 | 101 | | { |
| | 1 | 102 | | return false; |
| | | 103 | | } |
| | 9 | 104 | | } |
| | | 105 | | |
| | 7 | 106 | | targetSimpleList.AddWithExpandCheck(targetItem); |
| | 7 | 107 | | return true; |
| | 8 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Add an object reference to the <see cref="SimpleList{T}" /> with checking the internal size (expanding a |
| | | 112 | | /// making sure that the reference is not already contained in the <see cref="SimpleList{T}" />. |
| | | 113 | | /// Does not prevent addition of different objects for which Equals returns true. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" /> to add to.</param> |
| | | 116 | | /// <param name="targetReference">The target class object to add.</param> |
| | | 117 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 118 | | /// <returns>true/false if the operation was able to add the reference successfully.</returns> |
| | | 119 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 120 | | public static bool AddWithExpandCheckUniqueReference<T>(ref this SimpleList<T> targetSimpleList, T targetReferen |
| | | 121 | | where T : class |
| | 8 | 122 | | { |
| | 8 | 123 | | if (targetSimpleList.ContainsReference(targetReference)) |
| | 1 | 124 | | { |
| | 1 | 125 | | return false; |
| | | 126 | | } |
| | | 127 | | |
| | 7 | 128 | | targetSimpleList.AddWithExpandCheck(targetReference); |
| | 7 | 129 | | return true; |
| | 8 | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// <para>Does <paramref name="targetSimpleList" /> contain <paramref name="targetItem" />?</para> |
| | | 134 | | /// </summary> |
| | | 135 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 136 | | /// <param name="targetItem">The target class object to look for.</param> |
| | | 137 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 138 | | /// <returns>true/false</returns> |
| | | 139 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 140 | | public static bool Contains<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : IEquatable<T> |
| | 0 | 141 | | { |
| | 0 | 142 | | int length = targetSimpleList.Count; |
| | 0 | 143 | | T[] array = targetSimpleList.Array; |
| | | 144 | | |
| | 0 | 145 | | for (int i = 0; i < length; i++) |
| | 0 | 146 | | { |
| | 0 | 147 | | if (array[i].Equals(targetItem)) |
| | 0 | 148 | | { |
| | 0 | 149 | | return true; |
| | | 150 | | } |
| | 0 | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | return false; |
| | 0 | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// <para>Does <paramref name="targetSimpleList" /> contain <paramref name="targetItem" />?</para> |
| | | 158 | | /// </summary> |
| | | 159 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 160 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 161 | | /// <param name="targetItem">The target class object to look for.</param> |
| | | 162 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 163 | | /// <returns>true/false</returns> |
| | | 164 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 165 | | public static bool ContainsItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 22 | 166 | | { |
| | 22 | 167 | | int length = targetSimpleList.Count; |
| | 22 | 168 | | T[] array = targetSimpleList.Array; |
| | | 169 | | |
| | 96 | 170 | | for (int i = 0; i < length; i++) |
| | 32 | 171 | | { |
| | 32 | 172 | | if (array[i] == targetItem) |
| | 6 | 173 | | { |
| | 6 | 174 | | return true; |
| | | 175 | | } |
| | 26 | 176 | | } |
| | | 177 | | |
| | 16 | 178 | | return false; |
| | 22 | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// <para>Does <paramref name="targetSimpleList" /> contain <paramref name="targetItem" />?</para> |
| | | 183 | | /// </summary> |
| | | 184 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 185 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 186 | | /// <param name="targetItem">The target class object to look for.</param> |
| | | 187 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 188 | | /// <returns>true/false</returns> |
| | | 189 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 190 | | public static bool ContainsValue<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : IEquatable< |
| | 2 | 191 | | { |
| | 2 | 192 | | int length = targetSimpleList.Count; |
| | 2 | 193 | | T[] array = targetSimpleList.Array; |
| | | 194 | | |
| | 12 | 195 | | for (int i = 0; i < length; i++) |
| | 5 | 196 | | { |
| | 5 | 197 | | if (array[i].Equals(targetItem)) |
| | 1 | 198 | | { |
| | 1 | 199 | | return true; |
| | | 200 | | } |
| | 4 | 201 | | } |
| | | 202 | | |
| | 1 | 203 | | return false; |
| | 2 | 204 | | } |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// <para>Does <paramref name="targetSimpleList" /> contain <paramref name="targetItem" />?</para> |
| | | 208 | | /// </summary> |
| | | 209 | | /// <remarks>Ignores equality check and end up comparing object pointers.</remarks> |
| | | 210 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 211 | | /// <param name="targetItem">The target class object to look for.</param> |
| | | 212 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 213 | | /// <returns>true/false</returns> |
| | | 214 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 215 | | public static bool ContainsReference<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 22 | 216 | | { |
| | 22 | 217 | | int length = targetSimpleList.Count; |
| | 22 | 218 | | T[] array = targetSimpleList.Array; |
| | | 219 | | |
| | 96 | 220 | | for (int i = 0; i < length; i++) |
| | 32 | 221 | | { |
| | | 222 | | #pragma warning disable |
| | | 223 | | // ReSharper disable All |
| | 32 | 224 | | if ((System.Object)array[i] == (System.Object)targetItem) |
| | 6 | 225 | | { |
| | 6 | 226 | | return true; |
| | | 227 | | } |
| | | 228 | | // ReSharper restore All |
| | | 229 | | #pragma warning restore |
| | 26 | 230 | | } |
| | | 231 | | |
| | 16 | 232 | | return false; |
| | 22 | 233 | | } |
| | | 234 | | |
| | | 235 | | /// <summary> |
| | | 236 | | /// Find the first index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" />. |
| | | 237 | | /// </summary> |
| | | 238 | | /// <remarks>This will work for <see cref="string"/> comparisons.</remarks> |
| | | 239 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 240 | | /// <param name="targetItem">The object to be found.</param> |
| | | 241 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | | 242 | | /// <returns>The index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" /> backing array, |
| | | 243 | | public static int FirstIndexOf<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : IEquatable<T> |
| | 56 | 244 | | { |
| | 56 | 245 | | int length = targetSimpleList.Count; |
| | 168 | 246 | | for (int i = 0; i < length; i++) |
| | 84 | 247 | | { |
| | 84 | 248 | | if (targetSimpleList.Array[i].Equals(targetItem)) |
| | 56 | 249 | | { |
| | 56 | 250 | | return i; |
| | | 251 | | } |
| | 28 | 252 | | } |
| | 0 | 253 | | return -1; |
| | 56 | 254 | | } |
| | | 255 | | |
| | | 256 | | /// <summary> |
| | | 257 | | /// Find the first index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" />. |
| | | 258 | | /// </summary> |
| | | 259 | | /// <remarks>Ignores equality check and end up comparing object pointers. Do NOT use this for <see cref="string" |
| | | 260 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 261 | | /// <param name="targetItem">The object to be found.</param> |
| | | 262 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | | 263 | | /// <returns>The index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" /> backing array, |
| | | 264 | | public static int FirstIndexOfItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 0 | 265 | | { |
| | 0 | 266 | | int length = targetSimpleList.Count; |
| | 0 | 267 | | for (int i = 0; i < length; i++) |
| | 0 | 268 | | { |
| | 0 | 269 | | if (targetSimpleList.Array[i] == targetItem) |
| | 0 | 270 | | { |
| | 0 | 271 | | return i; |
| | | 272 | | } |
| | 0 | 273 | | } |
| | 0 | 274 | | return -1; |
| | 0 | 275 | | } |
| | | 276 | | |
| | | 277 | | /// <summary> |
| | | 278 | | /// Find the first index of <paramref name="targetValue" /> in <paramref name="targetSimpleList" />. |
| | | 279 | | /// </summary> |
| | | 280 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 281 | | /// <param name="targetValue">The value to be found.</param> |
| | | 282 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | | 283 | | /// <returns>The index of <paramref name="targetValue" /> in <paramref name="targetSimpleList" /> backing array, |
| | | 284 | | public static int FirstIndexOfValue<T>(ref this SimpleList<T> targetSimpleList, T targetValue) where T : struct |
| | 0 | 285 | | { |
| | 0 | 286 | | int length = targetSimpleList.Count; |
| | 0 | 287 | | for (int i = 0; i < length; i++) |
| | 0 | 288 | | { |
| | 0 | 289 | | if (targetSimpleList.Array[i].Equals(targetValue)) |
| | 0 | 290 | | { |
| | 0 | 291 | | return i; |
| | | 292 | | } |
| | 0 | 293 | | } |
| | | 294 | | |
| | 0 | 295 | | return -1; |
| | 0 | 296 | | } |
| | | 297 | | |
| | | 298 | | |
| | | 299 | | /// <summary> |
| | | 300 | | /// Find the last index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" />. |
| | | 301 | | /// </summary> |
| | | 302 | | /// <remarks>This will work for <see cref="string"/> comparisons.</remarks> |
| | | 303 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 304 | | /// <param name="targetItem">The object to be found.</param> |
| | | 305 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | | 306 | | /// <returns>The index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" /> backing array, |
| | | 307 | | public static int LastIndexOf<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : IEquatable<T> |
| | 0 | 308 | | { |
| | 0 | 309 | | int length = targetSimpleList.Count; |
| | 0 | 310 | | for (int i = length - 1; i >= 0; i--) |
| | 0 | 311 | | { |
| | 0 | 312 | | if (targetSimpleList.Array[i].Equals(targetItem)) |
| | 0 | 313 | | { |
| | 0 | 314 | | return i; |
| | | 315 | | } |
| | 0 | 316 | | } |
| | | 317 | | |
| | 0 | 318 | | return -1; |
| | 0 | 319 | | } |
| | | 320 | | |
| | | 321 | | /// <summary> |
| | | 322 | | /// Find the last index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" />. |
| | | 323 | | /// </summary> |
| | | 324 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 325 | | /// <param name="targetItem">The object to be found.</param> |
| | | 326 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | | 327 | | /// <returns>The index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" /> backing array, |
| | | 328 | | public static int LastIndexOfItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 0 | 329 | | { |
| | 0 | 330 | | int length = targetSimpleList.Count; |
| | 0 | 331 | | for (int i = length - 1; i >= 0; i--) |
| | 0 | 332 | | { |
| | 0 | 333 | | if (targetSimpleList.Array[i] == targetItem) |
| | 0 | 334 | | { |
| | 0 | 335 | | return i; |
| | | 336 | | } |
| | 0 | 337 | | } |
| | | 338 | | |
| | 0 | 339 | | return -1; |
| | 0 | 340 | | } |
| | | 341 | | |
| | | 342 | | /// <summary> |
| | | 343 | | /// Find the last index of <paramref name="targetValue" /> in <paramref name="targetSimpleList" />. |
| | | 344 | | /// </summary> |
| | | 345 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 346 | | /// <param name="targetValue">The value to be found.</param> |
| | | 347 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | | 348 | | /// <returns>The index of <paramref name="targetValue" /> in <paramref name="targetSimpleList" /> backing array, |
| | | 349 | | public static int LastIndexOfValue<T>(ref this SimpleList<T> targetSimpleList, T targetValue) where T : struct |
| | 0 | 350 | | { |
| | 0 | 351 | | int length = targetSimpleList.Count; |
| | 0 | 352 | | for (int i = length - 1; i >= 0; i--) |
| | 0 | 353 | | { |
| | 0 | 354 | | if (targetSimpleList.Array[i].Equals(targetValue)) |
| | 0 | 355 | | { |
| | 0 | 356 | | return i; |
| | | 357 | | } |
| | 0 | 358 | | } |
| | | 359 | | |
| | 0 | 360 | | return -1; |
| | 0 | 361 | | } |
| | | 362 | | |
| | | 363 | | /// <summary> |
| | | 364 | | /// <para>Removes the first <paramref name="targetItem" /> from the provided <paramref name="targetSimpleLis |
| | | 365 | | /// </summary> |
| | | 366 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 367 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" />.</param> |
| | | 368 | | /// <param name="targetItem">The target object to remove from the <paramref name="targetSimpleList" />.</param> |
| | | 369 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 370 | | /// <returns>true/false if an item was removed.</returns> |
| | | 371 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 372 | | public static bool RemoveFirstItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 2 | 373 | | { |
| | 2 | 374 | | int length = targetSimpleList.Count; |
| | 2 | 375 | | T[] array = targetSimpleList.Array; |
| | | 376 | | |
| | 18 | 377 | | for (int i = 0; i < length; i++) |
| | 8 | 378 | | { |
| | | 379 | | // Skip to next if its not what we are looking for |
| | 8 | 380 | | if (array[i] != targetItem) |
| | 7 | 381 | | { |
| | 7 | 382 | | continue; |
| | | 383 | | } |
| | | 384 | | |
| | 1 | 385 | | targetSimpleList.RemoveAt(i); |
| | 1 | 386 | | return true; |
| | | 387 | | } |
| | | 388 | | |
| | 1 | 389 | | return false; |
| | 2 | 390 | | } |
| | | 391 | | |
| | | 392 | | /// <summary> |
| | | 393 | | /// <para>Removes the first <paramref name="targetReference" /> from the provided <paramref name="targetSimp |
| | | 394 | | /// Only removes direct object references, i.e. equivalent strings at different memory addresses would not b |
| | | 395 | | /// </summary> |
| | | 396 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 397 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" />.</param> |
| | | 398 | | /// <param name="targetReference">The target object to remove from the <paramref name="targetSimpleList" />.</pa |
| | | 399 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 400 | | /// <returns>true/false if an object reference was removed.</returns> |
| | | 401 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 402 | | public static bool RemoveFirstReference<T>(ref this SimpleList<T> targetSimpleList, T targetReference) where T : |
| | 2 | 403 | | { |
| | 2 | 404 | | int length = targetSimpleList.Count; |
| | 2 | 405 | | T[] array = targetSimpleList.Array; |
| | | 406 | | |
| | 18 | 407 | | for (int i = 0; i < length; i++) |
| | 8 | 408 | | { |
| | | 409 | | #pragma warning disable |
| | | 410 | | // ReSharper disable All |
| | 8 | 411 | | if ((System.Object)array[i] == (System.Object)targetReference) |
| | 1 | 412 | | { |
| | 1 | 413 | | targetSimpleList.RemoveAt(i); |
| | 1 | 414 | | return true; |
| | | 415 | | } |
| | | 416 | | // ReSharper restore All |
| | | 417 | | #pragma warning restore |
| | 7 | 418 | | } |
| | | 419 | | |
| | 1 | 420 | | return false; |
| | 2 | 421 | | } |
| | | 422 | | |
| | | 423 | | /// <summary> |
| | | 424 | | /// <para>Removes all <paramref name="targetItem" /> from the provided <paramref name="targetSimpleList" />. |
| | | 425 | | /// </summary> |
| | | 426 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 427 | | |
| | | 428 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" />.</param> |
| | | 429 | | /// <param name="targetItem">The item to remove from the <paramref name="targetSimpleList" />.</param> |
| | | 430 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 431 | | /// <returns>true/false if items were removed.</returns> |
| | | 432 | | public static bool RemoveItems<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 1 | 433 | | { |
| | 1 | 434 | | int length = targetSimpleList.Count; |
| | 1 | 435 | | T[] array = targetSimpleList.Array; |
| | 1 | 436 | | bool removedItems = false; |
| | | 437 | | |
| | 14 | 438 | | for (int i = length - 1; i >= 0; i--) |
| | 6 | 439 | | { |
| | | 440 | | // Skip to next if its not what we are looking for |
| | 6 | 441 | | if (array[i] != targetItem) |
| | 4 | 442 | | { |
| | 4 | 443 | | continue; |
| | | 444 | | } |
| | | 445 | | |
| | 2 | 446 | | targetSimpleList.RemoveAt(i); |
| | 2 | 447 | | removedItems = true; |
| | 2 | 448 | | } |
| | | 449 | | |
| | 1 | 450 | | return removedItems; |
| | 1 | 451 | | } |
| | | 452 | | |
| | | 453 | | /// <summary> |
| | | 454 | | /// <para>Removes all instances of references to <paramref name="targetReference" /> from the provided <para |
| | | 455 | | /// Only removes direct object references, i.e. equivalent strings at different memory addresses would not b |
| | | 456 | | /// </summary> |
| | | 457 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 458 | | |
| | | 459 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" />.</param> |
| | | 460 | | /// <param name="targetReference">The object reference to remove from the <paramref name="targetSimpleList" />.< |
| | | 461 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 462 | | /// <returns>true/false if any references were removed.</returns> |
| | | 463 | | public static bool RemoveReferences<T>(ref this SimpleList<T> targetSimpleList, T targetReference) where T : cla |
| | 1 | 464 | | { |
| | 1 | 465 | | int length = targetSimpleList.Count; |
| | 1 | 466 | | T[] array = targetSimpleList.Array; |
| | 1 | 467 | | bool removedItems = false; |
| | | 468 | | |
| | 14 | 469 | | for (int i = length - 1; i >= 0; i--) |
| | 6 | 470 | | { |
| | | 471 | | #pragma warning disable |
| | | 472 | | // ReSharper disable All |
| | 6 | 473 | | if ((System.Object)array[i] == (System.Object)targetReference) |
| | 2 | 474 | | { |
| | 2 | 475 | | targetSimpleList.RemoveAt(i); |
| | 2 | 476 | | removedItems = true; |
| | 2 | 477 | | } |
| | | 478 | | // ReSharper restore All |
| | | 479 | | #pragma warning restore |
| | 6 | 480 | | } |
| | | 481 | | |
| | 1 | 482 | | return removedItems; |
| | 1 | 483 | | } |
| | | 484 | | |
| | | 485 | | /// <summary> |
| | | 486 | | /// <para>Removes the last <paramref name="targetItem" /> from the provided <paramref name="targetSimpleList |
| | | 487 | | /// </summary> |
| | | 488 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 489 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" />.</param> |
| | | 490 | | /// <param name="targetItem">The target object to remove from the <paramref name="targetSimpleList" />.</param> |
| | | 491 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 492 | | /// <returns>true/false if an item was removed.</returns> |
| | | 493 | | public static bool RemoveLastItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 2 | 494 | | { |
| | 2 | 495 | | int length = targetSimpleList.Count; |
| | 2 | 496 | | T[] array = targetSimpleList.Array; |
| | | 497 | | |
| | 18 | 498 | | for (int i = length - 1; i >= 0; i--) |
| | 8 | 499 | | { |
| | | 500 | | // Skip to next if its not what we are looking for |
| | 8 | 501 | | if (array[i] != targetItem) |
| | 7 | 502 | | { |
| | 7 | 503 | | continue; |
| | | 504 | | } |
| | | 505 | | |
| | 1 | 506 | | targetSimpleList.RemoveAt(i); |
| | 1 | 507 | | return true; |
| | | 508 | | } |
| | | 509 | | |
| | 1 | 510 | | return false; |
| | 2 | 511 | | } |
| | | 512 | | |
| | | 513 | | /// <summary> |
| | | 514 | | /// <para>Removes the last reference to <paramref name="targetReference" /> from the provided <paramref name |
| | | 515 | | /// Only removes direct object references, i.e. equivalent strings at different memory addresses would not b |
| | | 516 | | /// </summary> |
| | | 517 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 518 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" />.</param> |
| | | 519 | | /// <param name="targetReference">The target object reference to remove from the <paramref name="targetSimpleLis |
| | | 520 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 521 | | /// <returns>true/false if an object reference was removed.</returns> |
| | | 522 | | public static bool RemoveLastReference<T>(ref this SimpleList<T> targetSimpleList, T targetReference) where T : |
| | 2 | 523 | | { |
| | 2 | 524 | | int length = targetSimpleList.Count; |
| | 2 | 525 | | T[] array = targetSimpleList.Array; |
| | | 526 | | |
| | 18 | 527 | | for (int i = length - 1; i >= 0; i--) |
| | 8 | 528 | | { |
| | | 529 | | #pragma warning disable |
| | | 530 | | // ReSharper disable All |
| | 8 | 531 | | if ((System.Object)array[i] == (System.Object)targetReference) |
| | 1 | 532 | | { |
| | 1 | 533 | | targetSimpleList.RemoveAt(i); |
| | 1 | 534 | | return true; |
| | | 535 | | } |
| | | 536 | | // ReSharper restore All |
| | | 537 | | #pragma warning restore |
| | 7 | 538 | | } |
| | | 539 | | |
| | 1 | 540 | | return false; |
| | 2 | 541 | | } |
| | | 542 | | } |
| | | 543 | | } |